﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace Ahi.Base
{
    public partial class GirdiForm : BaseForm
    {
        //public T ReturnValue;
        private Control editor = null;

        public GirdiForm()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            //if (typeof(T) == typeof(int) ||
            //    typeof(T) == typeof(string) ||
            //    typeof(T) == typeof(decimal) ||
            //    typeof(T) == typeof(double))
            //{
            //    editor = new TextEdit();
            //}
            //else if (typeof(T) == typeof(DateTime))
            //{
            //    editor = new DateEdit();
            //}

            editor.Width = panelControlContainer.Width - panelControlContainer.Padding.Left * 2;
            editor.TabIndex = 0;
            panelControlContainer.Controls.Add(editor);
        }

        public static DialogResult ShowGirdiDialog<T>(ref T result, string message = "Lütfen değer giriniz:")
        {
            GirdiForm frm = new GirdiForm();
            if (typeof(T) == typeof(int) ||
                typeof(T) == typeof(string) ||
                typeof(T) == typeof(decimal) ||
                typeof(T) == typeof(double))
            {
                frm.editor = new TextEdit();
            }
            else if (typeof(T) == typeof(DateTime))
            {
                frm.editor = new DateEdit();
            }


            frm.labelMessage.Text = message;
            if (frm.editor != null)
                frm.editor.Focus();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                if (typeof(T) == typeof(int) ||
                    typeof(T) == typeof(string) ||
                    typeof(T) == typeof(decimal) ||
                    typeof(T) == typeof(double))
                {
                    result = (T)(frm.editor as TextEdit).EditValue;
                }
                else if (typeof(T) == typeof(DateTime))
                {
                    result = (T)(frm.editor as DateEdit).EditValue;
                }
                return DialogResult.OK;
            }
            else
            {
                result = default(T);
                return DialogResult.Cancel;
            }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.OK;
        }
    }
}
